#!/bin/bash

cd "$(dirname "$0")"      || exit 1

source ./LOCAL_ENV        || exit 1
export BATS_JUNIT_CLASS=$TEST_DIR
source ./common/TEST_ENV  || exit 1
source ./common/functions || exit 1


for i in "$@"; do
    case $i in
        -c=*|--compiler=*)
        COMPILER_FAMILIES="${i#*=}"
        shift
        ;;
        -m=*|--mpi=*)
        MPI_FAMILIES="${i#*=}"
        shift
        ;;
        -t|--tap)
        BATS_OPTS="$BATS_OPTS -t"
        shift
        ;;
        -x|--xml)
        BATS_OPTS="$BATS_OPTS -x"
        shift
        ;;
        *)
            echo "$0: Unknown option - $*"
            # unknown option
        ;;
    esac
done

for compiler in $COMPILER_FAMILIES ; do
    for mpi in $MPI_FAMILIES ; do

        echo " "
        echo " "
        echo "-------------------------------------------------------"
        echo "Apps: HPCG tests: $compiler-$mpi"
        echo "-------------------------------------------------------"

        rm -f HPCG*.yaml
	rm -f hpcg*.txt
	rm -f job.*.out

	module purge          || exit 1
	module load prun      || exit 1
	module load $compiler || exit 1
	module load $mpi      || exit 1

        ./build $BATS_OPTS
        ./rm_execution_single_host $BATS_OPTS
        ./rm_execution_multi_host $BATS_OPTS

        # save .log files for potential integration with CI
        save_logs_mpi_family . $compiler $mpi
	mv HPCG*.yaml family-$compiler-$mpi
	mv hpcg*.txt  family-$compiler-$mpi
	mv job.*.out  family-$compiler-$mpi
    done
done

if [ -r log.HPCG ]; then
	echo " "
	echo " "
	echo "-------------------------------------------------------"
	echo "Apps: HPCG tests: Results Summary"
	echo "-------------------------------------------------------"
	print "%-24s %-10s %-10s %16s %16s\n" \
	      Run_ID Compiler MPI_Stack Ref_GFLOP/s Curr_GFLOP/s
	cat log.HPCG

	mkdir -p logs
	cat log.HPCG >> logs/log.HPCG
	rm log.HPCG
fi
